// pages/real/detailsReal/index.js
import Toast from '../../../vant-weapp/dist/toast/toast'
const app = getApp()
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showExplan:false,
    isIphoneX: app.globalData.isIphoneX,
    isCollect:false,
    isLike:false,
    currentIndex:0,
    userInfo:"",
    phone:"",
    detailId:"",
    detailList:{},
    topImgUrl:"https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GIF",
    programmeName:"",
    designText:"",
    scene:"",
    pictureIndex:1,
    capBarMoveFlag:false,
    allSpaceImagList:[],
    recommendList:[],
    imgList:[],
    labelBtn:[],
    detailed:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('onLoad')
    const  userInfo = wx.getStorageSync('userInfo')
    const unionid=wx.getStorageSync("unionid")
    const phone = wx.getStorageSync('phone')
    const {  id , scene=null} = options
    this.setData({ userInfo, phone ,scene,detailId:id})
    if(scene){
      if(!userInfo){
        this.toLoginPage()
      }else{
         const str={
        id:scene ,
        createBy:unionid
      }
      this.hendleGetCodeList(str)
      }
     
    }else{
      this.handleGetRealDetailList()
    }
  },
  hendleGetCodeList(str){
    utils.$http(app.globalData.api.getRealContriGroundGetByCode, str, 'GET', true, 'JSON').then(res => {
      if(res.success){
        const arr=[]
        const d=res.data
       Object.keys(d).forEach(item=>{
         if(d[item]){
           if(item==='familyNumber'){
              arr[0]=d[item]
           }else if(item==='province' || item==='city' || item==='county'){
            arr[1]=d['province']+' '+'·'+' '+d['city']+' '+'·'+' '+d['county']
           }else if(item==='style'){
            arr[2]=d[item]
           }
         }
       })
       const imgs=res.data.roomRealDate.map(item=>{
         item.currentIndex=0
         return item
       })
        this.setData({
          detailList:res.data,
          isCollect:res.data.isKeep==='1'?true:false,
          isLike:res.data.isLike==='1'?true:false,
          labelBtn:arr,
          allSpaceImagList:imgs
        })
        utils.$http(app.globalData.api.getRealMyGroundBrowse, {
          id:d.id,
          openId:this.data.userInfo.unionId
        }, 'GET', true, 'JSON').then(res => {
            if(res.success){
            }
            
        })
      }
    })
  },
  switchShowDes(){
    this.setData({
      showExplan:!this.data.showExplan
    })
  },
  prveImg(e){
    const {arr,index}=e.currentTarget.dataset
    const {allSpaceImagList}=this.data
    let cur=0
    const len=arr.realImange && arr.realImange.length
    if(arr.currentIndex===0){
      cur=len-1
    }else{
      cur=arr.currentIndex-1
    }
   const newList = allSpaceImagList.map((item,indexs)=>{
     if(indexs===index){
       item.currentIndex=cur
     }
     return item
   })
    this.setData({
      allSpaceImagList:newList
    })
  },
  nextImg(e){
    const {arr,index}=e.currentTarget.dataset
    const {allSpaceImagList}=this.data
    let cur=0
    const len=arr.realImange && arr.realImange.length
    if(arr.currentIndex===len-1){
      cur=0
    }else{
      cur=arr.currentIndex+1
    }
   const newList = allSpaceImagList.map((item,indexs)=>{
     if(indexs===index){
       item.currentIndex=cur
     }
     return item
   })
    this.setData({
      allSpaceImagList:newList
    })
  },
  handleGetRealImgs(index){
    this.setData({
      imgList:this.data.detailList.roomRealDate[index].realImange,
      pictureIndex:1,
    })
  },
  toLoginPage(){
    const {userInfo , phone,scene,detailId } = this.data
    let message = ''
    if(!userInfo && !phone) {
      message = '未登陆，是否跳到登陆页登陆'
    }
    wx.showModal({
      title: '提示',
      content: message,
      success:function(res) {
        if(res.confirm) {
          wx.navigateTo({
            url: '/pages/mine/register/index'
          });
        } else if (res.cancel) {
          wx.switchTab({
            url: '/pages/real/homepage/index'
          });
        }
      }
    })
  },
  handleLikeClick(){
    const {isLike , detailList ,userInfo} =this.data
    const str={
      pid:detailList.id,
      userName:userInfo.unionId
    }
    if(!isLike){
      utils.$http(app.globalData.api.getRealMyGroundLikeRecords, str, 'GET', true, 'JSON').then(res => {
        if(res.success){
           wx.showToast({ icon: 'success', title: '点赞成功', duration: 1000 })
          this.setData({
            isLike:true
          })
        }
       
      })
    }else{
      utils.$http(app.globalData.api.getRealMyGroundCancelLikeRecords, str, 'GET', true, 'JSON').then(res => {
        if(res.success){
        wx.showToast({ icon: 'success', title: '取消点赞成功', duration: 1000 })
        this.setData({
          isLike:false
        })
      }
      })
    }
  },
  handleCollectClick(){
    const {isCollect , detailList ,userInfo} =this.data
    const str={
      pid:detailList.id,
      userName:userInfo.unionId
    }
    if(!isCollect){
      utils.$http(app.globalData.api.getRealMyGroundCollectRecords, str, 'GET', true, 'JSON').then(res => {
        if(res.success){
        wx.showToast({ icon: 'success', title: '收藏成功', duration: 1000 })
        this.setData({
          isCollect:true
        })
      }
      })
    }else{
      utils.$http(app.globalData.api.getRealMyGroundCancelCollectRecords, str, 'GET', true, 'JSON').then(res => {
        if(res.success){
        wx.showToast({ icon: 'success', title: '取消收藏成功', duration: 1000 })
        this.setData({
          isCollect:false
        })
      }
      })
    }
  },
  goDesignPage(){
    wx.navigateTo({
      url: '/pages/design/index/index',
    })
  },
  //跳转方案详情
  gotoProgrameDetail(e){
    let id = e.currentTarget.id
    wx.navigateTo({
      url: `/pages/real/detailsReal/index?id=${id}`,
    })
  },
  //获取实景落地详情信息
handleGetRealDetailList(){
  const unionid=wx.getStorageSync("unionid")
  //实景落地详情页面
  utils.$http(app.globalData.api.getRealMyGroundBrowse, {
    id:this.data.detailId,
    openId:unionid
  }, 'GET', true, 'JSON').then(res => {
      if(res.success){
      }
      
  })
  const userInfo=wx.getStorageSync("userInfo")
  const str={
    id:this.data.detailId,
    userName:unionid
  }
  utils.$http(app.globalData.api.getRealActualTemplateDetail, str, 'GET', true, 'JSON').then(res => {
    // console.log(res)
    if(res.success){
      const arr=[]
      const d=res.data
     Object.keys(d).forEach(item=>{
       if(d[item]){
         if(item==='familyNumber'){
            arr[0]=d[item]
         }else if(item==='province' || item==='city' || item==='county'){
          arr[1]=d['province']+' '+'·'+' '+d['city']+' '+'·'+' '+d['county']
         }else if(item==='style'){
          arr[2]=d[item]
         }
       }
     })
     const imgs=res.data.roomRealDate.map(item=>{
       item.currentIndex=0
       return item
     })
      this.setData({
        detailList:res.data,
        isCollect:res.data.isKeep==='1'?true:false,
        isLike:res.data.isLike==='1'?true:false,
        labelBtn:arr,
        allSpaceImagList:imgs
      })
    }
  })
  utils.$http(app.globalData.api.getRealActualLikeRoomList, str, 'GET', true, 'JSON').then(res => {
      if(res.success){
        this.setData({
          recommendList:res.data.slice(0,3)
        })
      }
      // console.log(this.data.recommendList)
  })
},
//跳转全屋
gotopanoramaUrl(e){
let url = e.currentTarget.dataset.url
wx.navigateTo({
  url: `/pages/H5/main/index?url=${encodeURIComponent(url)}`,
})
},
// 获取当前轮播图的index
onSlideChangeEnd: function (e) {
  var that = this;
  const index=e.detail.current
  if(e.detail.source==='touch'){
    this.handleGetRealImgs(index)
  }
},
//现场图轮播index
onSlideChangeEnds(e){
  const index=e.detail.current
  this.setData({
    pictureIndex: index + 1
  })
},
 /**
   * 获取本地用户信息
  */
 getLocalUserInfo: function(){
  const userInfo = wx.getStorageSync('userInfo')
    if (userInfo){
      this.setData({ userInfo, phone: userInfo.phone  })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  // 分享好友
  handleShare() {
    if(this.data.userInfo) {
      this.setData({
        clickShareFlag: true
      })
      const routes='pages/real/detailsReal/index'
      const imageURl =  utils.filterURL(this.data.detailList.coverImageUrl)
      const url = '/pages/share/index?id=' + this.data.detailList.id + '&code=' + '123' + '&shareName=' + this.data.detailList.programmeName + '&url=' + imageURl + "&source=ads" + '&routes='+ routes +'&real=ground'
      const that = this
      wx.navigateTo({
        url: url,
        complete: function(e) {
          that.setData({
            clickShareFlag: false
          })
        }
      })
    } else {
      this.toLoginPage()
    }
  },
  onPageScroll(e){
    const isSatisfy = e.scrollTop  >= 100  // 高度大于100的时候 显示白底标签栏
    if(this.data.capBarMoveFlag == isSatisfy) { 
      return false;
    } else {
      this.setData({
        capBarMoveFlag: isSatisfy
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const  userInfo = wx.getStorageSync('userInfo')
    const sharerPhone = wx.getStorageSync('phone') || ''
    // console.log(userInfo)
    // console.log(sharerPhone)
    // const sharerPhone = wx.getStorageSync('phone') || ''
    const sharerOpenId = wx.getStorageSync('openId') 

    const imageUrl = this.data.detailList.coverImageUrl && utils.filterURL(this.data.detailList.coverImageUrl) 

    return {
      title: this.data.detailList.programmeName || '详情页',
      imageUrl: imageUrl,
      path: "pages/real/detailsReal/index?id=" + this.data.detailList.id + "&sharerPhone=" + sharerPhone + "&sharerOpenId=" + sharerOpenId
    }
  }
})